// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { companyBase } from "../../../../script/game/control/Item/ItemBase";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    company: companyBase = null;
    mode: number = 1; //转入
    money: number = 0;
    maxMoney: number = 0;
    start () {
        this.company = game.playerMgr.bag.companyBag.getItem(this._args.id);
        this.initData();
        this.addBtn();
    }
    addBtn() {
        this.touchEndEventScale(this.nodes.add, () => {
            this.money += Math.trunc(this.maxMoney * 0.1);
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.reduce, () => {
            this.money -= Math.trunc(this.maxMoney * 0.1);
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.ensure, () => {
            if(this.money <= 0) {
                return ;
            }
            if(this.mode) {
                game.playerMgr.calMoney(-this.money * 0.9);
                this.company.addMoney(this.money);
                ccs.winMgr.showWin("toast", {msg: `成功转入${ccs.gf.getOmitNum(this.money * 0.9)}元`})
            } else {
                game.playerMgr.calMoney(this.money * 0.9);
                this.company.costMoney(this.money);
                ccs.winMgr.showWin("toast", {msg: `成功转出${ccs.gf.getOmitNum(this.money * 0.9)}元`})
            }
            this.initData();
            ccs.mainEmitter.emit('upInfo');
            ccs.mainEmitter.emit('upCompany');
            this._close();
        })
        this.touchEndEventScale(this.nodes.turnIn, () => {
            this.mode = 1;
            this.initData();
            this.nodes.turnOut.active = true;
            this.nodes.turnIn.active = false;
            this.nodes.mode.getComponent(cc.Label).string = "转入";
        })
        this.touchEndEventScale(this.nodes.turnOut, () => {
            this.mode = 0;
            this.initData();
            this.nodes.turnOut.active = false;
            this.nodes.turnIn.active = true;
            this.nodes.mode.getComponent(cc.Label).string = "转出";
        })
    }
    initData() {
        if(this.mode) {
            this.maxMoney = game.playerMgr.heroInfo.money;
        } else {
            this.maxMoney = this.company.company.money;
        }
        if(this.maxMoney < 0) {
            this.maxMoney = 0;
        }
        this.limitCount();
    }
    onSlider(tar) {
        let num = tar.progress;
        this.money = Math.floor(this.maxMoney * num);
        this.limitCount();
    }
    limitCount() {
        this.nodes.money.getComponent(cc.Label).string =  ccs.gf.getOmitNum(this.money);
        this.nodes.maxMoney.getComponent(cc.Label).string = "最大：" + ccs.gf.getOmitNum(this.maxMoney) + "（10%税率）";
    }

    // update (dt) {}
}
